package com.iot.ic.controller;

import com.iot.ic.base.BaseController;
import com.iot.ic.common.Response;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.model.IllegalResult;
import com.iot.ic.model.RefereeInfo;
import com.iot.ic.service.IllegalResultService;
import com.iot.ic.service.IllegalResultShowService;
import com.iot.ic.service.LogService;
import com.iot.ic.service.RefereeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 成绩修改管理
 */
@RestController
@Api(description = "违规信息相关接口")
public class IllegalResultController extends BaseController{

    @Autowired
    private IllegalResultService illegalResultService;
    
    @Autowired
    private RefereeService refereeService;
    
    @Autowired
    private LogService logService;
    
    @Autowired
    private IllegalResultShowService illegalResultShowService;

    @Autowired
    SimpMessagingTemplate simpMessagingTemplate;

    @ApiOperation(value = "获取最近count条违规记录",notes = "不带count参数的情况下可以获取所有违规记录；teamId是参赛队Id，不带此参数可以获取全部队伍的违规记录")
    @GetMapping(value = "/illegalResults")
    public Response getIllegalResultList(@RequestParam(value = "count",required = false,defaultValue = "99999")Integer count,
                                         @RequestParam(value = "teamId",required = false,defaultValue = "-1")Integer teamId,
                                         @RequestParam(value = "startTime", required = false) String startTime,
                                         @RequestParam(value = "endTime", required = false) String endTime){
        Response response=new Response();
        List<Map<String,Object>> list=illegalResultShowService.getIllegalResultShowList(count, teamId, startTime, endTime);
        response.setResult(list);
        return response;
    }

    @ApiOperation(value = "获取最近count条违规记录，websocket请求",notes = "不带count参数的情况下可以获取所有违规记录；teamId是参赛队Id，不带此参数可以获取全部队伍的违规记录")
    @MessageMapping("/illegalResultsSocket")
    @SendToUser("/illegal/results")
    public void getIllegalResultListSocket(
            @Header(value = "user") String adminId,
            @Header(value = "count",required = false,defaultValue = "99999")Integer count,
            @Header(value = "teamId",required = false,defaultValue = "-1")Integer teamId,
            @Header(value = "startTime", required = false) String startTime,
            @Header(value = "endTime", required = false) String endTime)
    {
        Response response=new Response();
        List<Map<String,Object>> list=illegalResultService.getIllegalResultList(count, teamId, startTime, endTime);
        response.setResult(list);

        simpMessagingTemplate.convertAndSendToUser(adminId, "/illegal/results", response);
    }

    @ApiOperation(value = "根据id修改违规记录")
    @PutMapping("/users/{userId}/illegalResults/{illegalResultId}")
    public Response modIllegalResult(@PathVariable("userId")int userId,
                                     @PathVariable("illegalResultId")int illegalResultId,
                                     @RequestBody @Valid IllegalResult illegalResult){
        Response response=new Response();
        Map<String,Object> resultMap = illegalResultService.getStadiumScenesByIllegal(illegalResultId);
        String stadiumName = resultMap.get("stadiumName").toString();
        String scenesName = resultMap.get("scenesName").toString();
        String illegalName = resultMap.get("illegalName").toString();
        RefereeInfo refereeInfo = refereeService.getRefereeInfo(userId);
        logService.addLogs(refereeInfo.getRefereeNo(), refereeInfo.getRefereeName(), new Date(), "修改"+stadiumName+scenesName+illegalName+"的成绩", "");
        illegalResult.setIllegalResultId(illegalResultId);
        int result=illegalResultService.modIllegalResult(illegalResult);
        if(result==1)
            response.setResult(result);
        else
            response.setInfos(ReturnCode.Err_RequestFailed);
        return response;
    }

    @ApiOperation(value = "分页查询修改成绩的操作记录")
    @PostMapping("/user/illegalLogs")
    public Response selectAllLogs(
            @RequestParam(value = "pageNo",required = false,defaultValue = "1")Integer pageNo,
            @RequestParam(value = "pageSize",required = false,defaultValue = "0")Integer pageSize
    ){
        pageNo = pageNo < 1 ? 1:pageNo;
        pageSize = pageSize < 0 ? 0:pageSize;

        List result = logService.selectAll((pageNo - 1)*pageSize, pageSize);
        int sum = logService.selectAllNumber();

        Response response=new Response();
        response.setResult(result);
        response.setNumReturn(sum);
        return response;
    }

}
